﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using YXShop.Common;
using System.Collections.Generic;

namespace YXShop.Web.admin.store
{
    public partial class storemessage_list : System.Web.UI.Page
    {
        YXShop.BLL.Store.StoreMessage bll = new YXShop.BLL.Store.StoreMessage();
        protected void Page_Load(object sender, EventArgs e)
        {
            YXShop.Common.PromptInfo.Popedom("014003001");

            if(!IsPostBack)
            {
                if (ChangeHope.WebPage.PageRequest.GetFormString("Option") != string.Empty && ChangeHope.WebPage.PageRequest.GetFormString("id") != "")
                {
                    
                    string types = Request["Option"].Trim();
                    string id = ChangeHope.WebPage.PageRequest.GetFormString("id");
                    if (types == "del")
                    {
                        if (YXShop.Common.PromptInfo.Message("014003003") != "ok")
                        {
                            bll.Delete(Convert.ToInt32(id));
                        }
                        else
                        {
                            Response.Write("no");
                        }
                    }
                    if (types == "delAll")
                    {
                        if (YXShop.Common.PromptInfo.Message("014003003") != "ok")
                        {
                        bll.DeleteAll(id);
                         }
                        else
                        {
                            Response.Write("no");
                        }
                    }
                    Response.End();
                    return;
                }
                this.lblList.Text = GetList("");
            }
        }

        #region 绑定
        private string GetList(string where)
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            where = where + getWhereStr();
            ChangeHope.DataBase.DataByPage dataPage = bll.GetList(where);
            table.AddHeadCol("", "");
            table.AddHeadCol("","标题");
            table.AddHeadCol("","发送时间");
            table.AddHeadCol("","来源");
            table.AddHeadCol("", "类别");
            table.AddHeadCol("","发送用户");
            table.AddHeadCol("","操作");
            table.AddRow();
            if (dataPage.DataReader != null)
            {
                int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
                if (curpage < 0)
                {
                    curpage = 1;
                }
                int count = 0;
                YXShop.Common.SysParameter sp=new YXShop.Common.SysParameter();
                YXShop.Model.Admin.AdminInfo admin = YXShop.Common.AdministrorManager.Get();
                while (dataPage.DataReader.Read())
                {
                    count++;
                    string No = (15 * (curpage - 1) + count).ToString();
                    table.AddCol("<input id=\"cbTm\"  type=\"checkbox\" value=\"" + dataPage.DataReader["id"].ToString() + "\" />");
                    table.AddCol(dataPage.DataReader["messagetitle"].ToString());
                    table.AddCol(Convert.ToDateTime(dataPage.DataReader["sendertime"]).ToString());
                    int messageType = Convert.ToInt32(dataPage.DataReader["messagetype"]);
                    if (messageType == 0)
                        table.AddCol("总部消息");
                    else if (messageType == 1)
                        table.AddCol("销售中心消息");
                    int messageCategory = Convert.ToInt32(dataPage.DataReader["messagecategory"]);
                    table.AddCol(ValuePair.GetMessageCategory(messageCategory));
                    table.AddCol(GetUserName(Convert.ToInt32(dataPage.DataReader["senderid"])));
                    string strUrlView = YXShop.TemplateAction.Common.UrlStr("storemessage_view", "id=" + dataPage.DataReader["id"].ToString(), "admin/store/");
                    string strUrlEdit = YXShop.TemplateAction.Common.UrlStr("storemessage_edit", "id=" + dataPage.DataReader["id"].ToString(), "admin/store/");
                    bool canDelete = false;
                    if (admin.AdminType == 1)
                    {
                        canDelete = true;

                    }
                    else if (admin.AdminType == 2)
                    {
                        if (messageType == 0)
                            canDelete = false;
                        else
                            canDelete = true;
                    }
                    else if (admin.AdminType == 3)
                    {
                        canDelete = false;
                    }
                    if (canDelete)
                        table.AddCol("<a href=\"" + strUrlView + "\">查看</a>&nbsp;<a href=\"" + strUrlEdit + "\">修改</a>&nbsp;<a href=\"#\" onclick='Del(" + dataPage.DataReader["id"] + ")'>删除</a>");
                    else
                        table.AddCol("<a href=\"" + strUrlView + "\">查看</a>");
                    table.AddRow();
                }
              
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            return view;
        }
        #endregion

        #region  获取数据
        /// <summary>
        /// 用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        private string GetUserName(int userId)
        {
            YXShop.BLL.Admin.Administrators userBll = new BLL.Admin.Administrators();
            YXShop.Model.Admin.Administrators  user = userBll.GetModel(userId);
            if (user != null)
            {
                return user.Name;
            }
            else
            {
                return "";
            }

        }

        #endregion

        #region 查询
        protected void linkQuery_Click(object sender, EventArgs e)
        {
            string strWhere="";               
            this.lblList.Text = GetList(strWhere);
        }
        #endregion

        #region 获取查询条件和排序条件
        private string getWhereStr()
        {
            string result = "";
            YXShop.Model.Admin.AdminInfo admin = YXShop.Common.AdministrorManager.Get();
            if (admin != null)
            {
                if (admin.AdminType == 1)
                {
                    result = "";
                }
                else if(admin.AdminType==2)
                {
                    //可以看到总部发出的消息以及当前用户管理的销售中心的消息
                    YXShop.BLL.Admin.Administrators adminBLL=new BLL.Admin.Administrators ();
                    List<YXShop.Model.Store.StoreCenter> centers = adminBLL.GetStoreCenterAdmin_StoreCenter(admin.AdminId);
                    List<int> ids = new List<int>();
                    foreach (YXShop.Model.Store.StoreCenter center in centers)
                    {
                        ids.Add(center.Sid.Value);
                    }
                    result = " and (messagetype=0 " + ChangeHope.Common.Util.GetWhereInClause(ids, "storecenterid", "or")+ ") ";
                }
                else if (admin.AdminType == 3)
                {
                    //可以看到总部发出的消息，以及当前用户管理的门店，所在的销售中心的消息。
                    YXShop.BLL.Admin.Administrators adminBLL = new BLL.Admin.Administrators();
                    List<YXShop.Model.Store.StoreCenter> centers = adminBLL.GetStoreCenterOfAdmin_Store(admin.AdminId);
                    List<int> ids = new List<int>();
                    foreach (YXShop.Model.Store.StoreCenter center in centers)
                    {
                        ids.Add(center.Sid.Value);
                    }
                    result = " and (messagetype=0 " + ChangeHope.Common.Util.GetWhereInClause(ids, "storecenterid", "or") + ") "; 
                }
            }
            return result;
        }


        #endregion
    }
}
